﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace mjc.sync.rules
{
    public class RegexSyncFilter : SyncFilter
    {
        private String _pattern;
        private Regex rgx;

        public String RegexPattern
        {
            get { return _pattern; }
            set 
            { 
                _pattern = value;
                if (value == null || String.Empty.Equals(value))
                    rgx = null;
                else
                    rgx = new Regex(_pattern, RegexOptions.IgnoreCase);
            }
        }

        public RegexSyncFilter()
        {
        }

        public RegexSyncFilter(String pattern) {
            this.RegexPattern = pattern;
            this.rgx = new Regex(pattern,RegexOptions.IgnoreCase);
        }

        public bool Matches(String s) {
            if (rgx == null) return true; // Null will match anything...
            
            if (s == null) return false;

            return rgx.IsMatch(s);
        }
    }
}
